/**
 * 
 */
package pms.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import pms.dao.NotenDao;
import pms.om.Noten;
import pms.om.PersonHatNoten;
import pms.om.Stimme;



/** 
 * <p>
 * Controller to generate the Home Page basics to be rendered by a view. 
 * It extends the convenience class AbstractController that encapsulates most 
 * of the drudgery involved in handling HTTP requests. 
 * </p>
 * 
 * <p>
 * ModelAndView contains
 * <ul>
 * <li>title: Will be used as a headline in the resulting view</li>
 * <li>message: Some hints for the user</li>
 * <li>noten: A list/set of noten</li>
 * <li>command: A command that will be used in the links of the list of "noten" (on the view)</li>
 * </ul>
 * The name of the selected view will be: 'list-noten.jsp'.
 * </p>
 * 
 * @see NotenDao
/**
 * @author roman.zaglauer@googlemail.com
 *
 */
public class AlleNoten extends AbstractController{
	private static Log log = LogFactory.getLog(AlleNoten.class);
	private NotenDao notenDao;
	
	
	
	/** Getter
	 * @return the notenDao
	 */
	public NotenDao getNotenDao() {
		return notenDao;
	}



	/** Setter
	 * @param notenDao the notenDao to set
	 */
	public void setNotenDao(NotenDao notenDao) {
		this.notenDao = notenDao;
	}



	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) throws Exception {
		log.info(httpServletRequest.getPathTranslated());
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Liste der Noten");
		mv.addObject("message", "Alle Noten im Notenarchivsystem.");
		
		
		int musikstueckid = Integer.parseInt(httpServletRequest.getParameter("musikstueckid"));
		mv.addObject("musikstueck", notenDao.getMusikstueckDao().findById(musikstueckid));
		
		List<Noten> alleNoten = notenDao.getMusikstueckDao().findNotenByMusikstueckId(musikstueckid);

//		List<PersonHatNoten> ausgeliehen = notenDao.getPersonHatNotenDao().findPHNByMusikstueckId(musikstueckid);
		List<PersonHatNoten> ausgeliehen = notenDao.findPHNByMusikstueckId(musikstueckid);
		
		
		
		List<Noten> nichtAusgeliehen = new ArrayList<Noten>();
		
		boolean flag = false;
		
		for (Noten noten : alleNoten) {
			for (PersonHatNoten personHatNoten : ausgeliehen) {
				if (personHatNoten.getNoten().getId() == noten.getId()) {
					flag = true;
					continue;
				}
			}
			if (flag == false) {
				nichtAusgeliehen.add(noten);
			} else {
				flag = false;
			}
		}
		
		mv.addObject("notenlist", nichtAusgeliehen);
		mv.addObject("edit", true);
		mv.setViewName("list-noten");
		return mv;
	}

}
